//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TridentWordAddIn.Common;
using Microsoft.Research.DataLayer;

namespace Microsoft.TridentWordAddIn.Server
{
    public class ServerHelper
    {

        /// <summary>
        /// Converts to job output.
        /// </summary>
        /// <param name="dataProduct">The data product.</param>
        /// <returns></returns>
        public static JobOutput ConvertToJobOutput(DataProduct dataProduct)
        {
            JobOutput jobOutput = new JobOutput();
            jobOutput.Id = (dataProduct as IObject).ID;
            jobOutput.FileName = dataProduct.Name + FileHelper.TryGetExtension(dataProduct.Type);
            jobOutput.Contents = dataProduct.Contents;
            jobOutput.IsDataProduct = dataProduct.IsFinal;

            return jobOutput;
        }

        /// <summary>
        /// Converts to shallow output.
        /// </summary>
        /// <param name="dataProduct">The data product.</param>
        /// <returns></returns>
        public static ShallowJobOutput ConvertToShallowOutput(DataProduct dataProduct)
        {
            ShallowJobOutput jobOutput = new ShallowJobOutput();
            jobOutput.Id = (dataProduct as IObject).ID;
            jobOutput.Name = dataProduct.Name;
            jobOutput.IsDataProduct = dataProduct.IsFinal;
            jobOutput.Extension = FileHelper.TryGetExtension(dataProduct.Type);

            return jobOutput;
        }

        /// <summary>
        /// Converts to workflow job status.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <returns>WorkflowJobStatus</returns>
        public static WorkflowJobStatus ConvertToWorkflowJobStatus(JobStatus status)
        {
            WorkflowJobStatus result;
            switch (status)
            {
                case JobStatus.Waiting:
                case JobStatus.Running:
                case JobStatus.Initializing:
                case JobStatus.StartPending:
                case JobStatus.ResumePending:
                case JobStatus.Paused:
                case JobStatus.PausePending:
                    result = WorkflowJobStatus.Processing;
                    break;
                case JobStatus.Completed:
                    result = WorkflowJobStatus.Finished;
                    break;
                case JobStatus.Aborted:
                case JobStatus.StopPending:
                    result = WorkflowJobStatus.Terminated;
                    break;
                default:
                    result = WorkflowJobStatus.Unknown;
                    break;
            }

            return result;
        }

        /// <summary>
        /// Converts to trident job.
        /// </summary>
        /// <param name="job">The job.</param>
        /// <returns></returns>
        public static WorkflowJob ConvertToWorkflowJob(Job job)
        {
            WorkflowJob workflowJob = new WorkflowJob();
            workflowJob.Id = (job as IObject).ID;
            workflowJob.Name = job.Name;
            workflowJob.WorkflowId = (job.ActivityInstances[0].Activity as IObject).ID;
            workflowJob.Description = job.ActivityInstances[0].Activity.Description;
            workflowJob.DateSubmitted = job.Created.When;
            workflowJob.DateCompleted = job.Updated.When;
            workflowJob.ScheduledBy = job.ScheduledBy.Name;
            workflowJob.Status = ConvertToWorkflowJobStatus(job.Status);

            return workflowJob;
        }
    }
}
